package com.top.web.hooker.common;

import android.content.Context;
import android.net.ConnectivityManager;

import java.lang.reflect.Method;

/**
 * Created by anye6488 on 2015/7/28.
 */
public class Network
{
    /**
     * 设置手机的移动数据
     */
    public static void setMobileDataEnabled(Context pContext, boolean pBoolean)
    {
        try
        {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            Class ownerClass = mConnectivityManager.getClass();
            Class[] argsClass = new Class[1];
            argsClass[0] = boolean.class;
            Method method = ownerClass.getMethod("setMobileDataEnabled", argsClass);
            method.invoke(mConnectivityManager, pBoolean);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 返回手机移动数据的状态
     *
     * @param pContext
     * @param arg      默认填null
     * @return true 连接 false 未连接
     */
    public static boolean getMobileDataEnabled(Context pContext)
    {
        try
        {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);

            Class ownerClass = mConnectivityManager.getClass();

            Method method = ownerClass.getMethod("getMobileDataEnabled");

            Boolean isOpen = (Boolean) method.invoke(mConnectivityManager);

            return isOpen;

        }
        catch (Exception e)
        {
            return false;
        }

    }
}
